Write a C++ program to prompt the user to input 3 integer values and print these values in Reverse [Descending] order

 

Assignment 3 (A)

Write a C++ program to prompt the user to input 3 integer values and print these values in Reverse [Descending] order.

Program Code with explanation [ Without loop ]:

1.    #include <iostream>

2.    using namespace std;

3.    int main()

4.    {

5.          int a1,a2,a3;

6.          cout<<"Accept three integer values to display Reverse [Descending] order";

7.          cout<<"\nAccept First Number ";

8.          cin>>a1;

9.          cout<<"\nAccept Second Number ";

10.       cin>>a2;

11.       cout<<"\nAccept third Number ";

12.       cin>>a3;

13.       cout<<"Display Reverse [Descending] order \n";

14.       // Sort numbers without using Loop.

15.       if((a1>a2)&(a1>a3))

16.       {

17.                   cout<<"\n"<<a1<<"\t";

18.                   if(a2>a3)

19.                               cout<<a2<<"\t"<<a3;

20.                   else

21.                               cout<<a3<<"\t"<<a2;

22.       }

23.       if((a2>a1)&(a2>a3))

24.       {

25.                   cout<<"\n"<<a2<<"\t";

26.                   if(a1>a3)

27.                               cout<<a1<<"\t"<<a3;

28.                   else

29.                               cout<<a3<<"\t"<<a1;

30.       }

31.       if((a3>a1)&(a3>a2))

32.       {

33.                   cout<<"\n"<<a3<<"\t";

34.                   if(a1>a2)

35.                               cout<<a1<<"\t"<<a2;

36.                   else

37.                               cout<<a2<<"\t"<<a1;

38.       }

39.       return 0;

40. }

Explanation of Code:

Line No. 1 and 2: include the Library file and stander input – output functions.

Line No. 3: main function header.

Line No. 4 and 40: Begin and end of main function respectively.

Line No. 5: Declare the three integer variables.

Line No. 6 to 12: Accept three integer numbers with appropriate massage.  

Line No. 13 and 14: Display massage “Display Reverse [Descending] order”.

Line No. 15: To check first integer number greater than other than two if it’s true then goes to Line No 17 and print first number.

Line No. 18: To check second  integer number greater than third integer number if it’s true then goes to Line No 19 and print second and third integer number respectively. Otherwise goes to 21 and print third and second integer number respectively.

Repeat same process for check second and third number at Line No 23 to 30 and 31 to 38 respectively.

Program Code for run:

#include <iostream>

using namespace std;

int main()

{

    int a1,a2,a3;

    cout<<"Accept three integer values to display Reverse [Descending] order";

    cout<<"\nAccept First Number ";

    cin>>a1;

    cout<<"\nAccept Second Number ";

    cin>>a2;

    cout<<"\nAccept third Number ";

    cin>>a3;

    cout<<"Display Reverse [Descending] order \n";

    // Sort numbers without using Loop.

    if((a1>a2)&(a1>a3))

    {

        cout<<"\n"<<a1<<"\t";

        if(a2>a3)

            cout<<a2<<"\t"<<a3;

        else

            cout<<a3<<"\t"<<a2;

    }

   

    if((a2>a1)&(a2>a3))

    {

        cout<<"\n"<<a2<<"\t";

        if(a1>a3)

            cout<<a1<<"\t"<<a3;

        else

            cout<<a3<<"\t"<<a1;

    }

   

    if((a3>a1)&(a3>a2))

    {

        cout<<"\n"<<a3<<"\t";

        if(a1>a2)

            cout<<a1<<"\t"<<a2;

        else

            cout<<a2<<"\t"<<a1;

    }

    return 0;

}

Output of Program:

Accept three integer values to display Reverse [Descending] order

Accept First Number  3

Accept Second Number  1

Accept third Number  2

Display Reverse [Descending] order

3          2          1


Comments